package com.sy.simpleegg.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.sy.simpleegg.R
import com.sy.simpleegg.adapter.RecordOrderAdapter
import com.sy.simpleegg.adapter.SpinnerAdapter
import com.sy.simpleegg.base.BaseMvpFragment
import com.sy.simpleegg.bean.RecordOrderBean
import com.sy.simpleegg.databinding.FragmentRecordBinding
import com.sy.simpleegg.ext.logFile
import com.sy.simpleegg.mvp.contract.RecordContract
import com.sy.simpleegg.mvp.presenter.RecordPresenter
import com.sy.simpleegg.ui.layout.TouchLinearLayout
import com.sy.simpleegg.util.CommonUtil
import com.sy.simpleegg.util.GsonUtil
import com.sy.simpleegg.util.SoftInputUtil

class RecordFragment :
    BaseMvpFragment<RecordContract.View, RecordContract.Presenter, FragmentRecordBinding>(),
    RecordContract.View {

    private var passDates: Array<String>? = null

    private var selectDatePosition = -1

    private var recordOrderAdapter: RecordOrderAdapter? = null

    companion object {
        fun getInstance(): RecordFragment = RecordFragment()
    }

    override fun createPresenter(): RecordContract.Presenter = RecordPresenter(lifecycleScope)

    override fun attachLayoutRes(): Int = R.layout.fragment_record

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        openEventBus = false
    }

    override fun initView(view: View) {
        super.initView(view)

        binding.spDate.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                if (selectDatePosition != position){
                    selectDatePosition = position
                    logFile("订单记录选中日期：${passDates?.get(position)}")
                    passDates?.let {
                        refreshOrders(it[selectDatePosition])
                    }
                }
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
        }

        binding.rlTouch.setTouchListener(object : TouchLinearLayout.TouchListener {
            override fun touch() {
                SoftInputUtil.hideSoftInput(binding.root)
                startCountDown()
            }
        })

    }

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
    ): FragmentRecordBinding {
        return FragmentRecordBinding.inflate(inflater, container, false)
    }

    override fun lazyLoad() {
        loadDate(7)
    }

    override fun onResume() {
        super.onResume()
        logFile("${javaClass.simpleName} onResume")
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden) {
            logFile("${javaClass.simpleName}隐藏")
        } else {
            logFile("${javaClass.simpleName}显示")
        }
    }

    override fun loadData(datas: MutableList<RecordOrderBean>) {
        try {
            logFile("加载订单数据：${GsonUtil.getInstance()?.toJson(datas)}")
            if (recordOrderAdapter == null){
                recordOrderAdapter = RecordOrderAdapter(datas, requireContext())
                binding.rvOrders.layoutManager = LinearLayoutManager(requireContext())
                binding.rvOrders.adapter = recordOrderAdapter
            }else{
                recordOrderAdapter?.refreshData(datas)
                recordOrderAdapter?.notifyDataSetChanged()
            }
        }catch (e: Exception){
            e.printStackTrace()
            logFile("加载订单记录数据异常：${e.message}")
        }
    }


    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    private fun loadDate(sum: Int){
        try {
            passDates = CommonUtil.getPassDate(sum, "yyyyMMdd").apply {
                binding.spDate.adapter = SpinnerAdapter(this)
                binding.spDate.setSelection(0)
            }
        }catch (e: Exception){
            e.printStackTrace()
        }
    }

    private fun refreshOrders(date: String){
        mPresenter?.getOrder(date)
    }



}